/**
 * @Description: 数据探索
 * @Author: Tom
 * @Date: 2020-02-25 16:13:38
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-03-26 15:16:20
 */
$(function () {
    // 调用jquery-editable-select
    $('#input-search').editableSelect({
        'effects': 'slide'
    });
    $('#input-search').show();
    // 设置按钮位置
    $('#hideBtn').css('left', $('#mainleft').offset().left + $('#mainleft').width())
    // 收起/展开左侧
    $('#hideBtn').on('click', function () {
        var $this = $(this);
        if ($this.find('div').hasClass('show')) {
            $this.find('div').removeClass('show');
            $('#mainleft').show();
            $('#hideBtn').css('left', $('#mainleft').offset().left + $('#mainleft').width());
            SetCatalogHeight();
        } else {
            $this.find('div').addClass('show');
            $('#mainleft').hide();
            $('#hideBtn').css('left', 0);
            $('#mainright').outerWidth($('#main').width() - 10);
        }
    });

    var dbName = '';
    var mySelect = $('#mySelect').val();
    var searchData = {};

    $('#dbName').on('change', function (e) {
        dbName = $(this).val();
        initData();
    });
    $('#mySelect').on('change', function (e) {
        mySelect = $(this).val();
        initData();
    });
    // 数据探索页面初始化
    function initData() {
        $('#input-search').val('');
        if (!searchData[dbName][mySelect]) {
            $.ajax({
                type: "post",
                async: false,
                url: "/BaseAshx/SmartStatistics/ExploreData.ashx",
                data: {
                    mtype: "ExploreData_InitialDataList",
                    dbname: dbName,
                    user: 'admin' // 假用户
                },
                datatype: "json",
                success: function (result) {
                    result = result.split('||');
                    // console.log(result);
                    if (result.length == 3) {
                        //result = '[{"TABLE_NAME":"A_QUOTACOMPARE"},{"TABLE_NAME":"A_QUOTACOMPARE_QX"},{"TABLE_NAME":"BASEDATA_TDFL_2D"},{"TABLE_NAME":"BASERPT_JCJSB"},{"TABLE_NAME":"CHANGE_2TO3"},{"TABLE_NAME":"CHANGE_2TO3_JS2"},{"TABLE_NAME":"COMPUTER_INFO2"},{"TABLE_NAME":"DATA_TABLERESULT"},{"TABLE_NAME":"DLTB"},{"TABLE_NAME":"DLTB_ZRBHQ"},{"TABLE_NAME":"DOMMETA"},{"TABLE_NAME":"FD_FILETYPE"},{"TABLE_NAME":"GPM_APP"},{"TABLE_NAME":"GPM_APPSORT"},{"TABLE_NAME":"GPM_BAR"},{"TABLE_NAME":"GPM_COMPUTER_INFO"},{"TABLE_NAME":"GPM_EMPAPP"},{"TABLE_NAME":"GPM_EMPMENU"},{"TABLE_NAME":"GPM_GROUP"},{"TABLE_NAME":"GPM_GROUPEMP"},{"TABLE_NAME":"GPM_GROUPMENU"},{"TABLE_NAME":"GPM_GROUPSTATION"},{"TABLE_NAME":"GPM_INFOPUSH"},{"TABLE_NAME":"GPM_LINK"},{"TABLE_NAME":"GPM_LINKSORT"},{"TABLE_NAME":"GPM_MENU"},{"TABLE_NAME":"GPM_STATIONMENU"},{"TABLE_NAME":"GPM_SYSRECORDS"},{"TABLE_NAME":"GPM_USERMENU"},{"TABLE_NAME":"GPM_USERRECORDS"},{"TABLE_NAME":"GPM_ZIXUN_TZGG"},{"TABLE_NAME":"GPM_ZIXUN_XXKB"},{"TABLE_NAME":"GPM_ZIXUN_ZDGZQK"},{"TABLE_NAME":"GPM_ZIYUAN_INFO"},{"TABLE_NAME":"PORT_DATA_CUSTOMIZATION"},{"TABLE_NAME":"PORT_DATA_SHENHE"},{"TABLE_NAME":"PORT_DEPT"},{"TABLE_NAME":"PORT_DEPTDUTY"},{"TABLE_NAME":"PORT_DEPTEMP"},{"TABLE_NAME":"PORT_DEPTEMPSTATION"},{"TABLE_NAME":"PORT_DEPTSTATION"},{"TABLE_NAME":"PORT_DEPTTYPE"},{"TABLE_NAME":"PORT_DUTY"},{"TABLE_NAME":"PORT_EMP"},{"TABLE_NAME":"PORT_EMPDEPT"},{"TABLE_NAME":"PORT_EMPSTATION"},{"TABLE_NAME":"PORT_EMPTYPE"},{"TABLE_NAME":"PORT_GENDER"},{"TABLE_NAME":"PORT_GJEMP"},{"TABLE_NAME":"PORT_MAPNONAME"},{"TABLE_NAME":"PORT_ORGAN"},{"TABLE_NAME":"PORT_STATION"},{"TABLE_NAME":"PORT_STATIONTYPE"},{"TABLE_NAME":"PORT_UNIT"},{"TABLE_NAME":"PORT_ZC"},{"TABLE_NAME":"PORT_ZCYH"},{"TABLE_NAME":"PORT_ZXJB"},{"TABLE_NAME":"PUBLIC_DIC"},{"TABLE_NAME":"REMOTE_CONNECT_HISTORY2"},{"TABLE_NAME":"SYS_ENCFG"},{"TABLE_NAME":"SYS_ENSAPPCFG"},{"TABLE_NAME":"SYS_ENUM"},{"TABLE_NAME":"SYS_FILEMANAGER"},{"TABLE_NAME":"SYS_SERIAL"},{"TABLE_NAME":"SYS_USERLOGT"},{"TABLE_NAME":"SYS_USERREGEDIT"},{"TABLE_NAME":"TB_ANANLYSIS_FLOWDIRECTION"},{"TABLE_NAME":"TB_CHANGE_2DIAOHUIZONG"},{"TABLE_NAME":"TB_CHANGE_2TO3"},{"TABLE_NAME":"TB_CHANGE_2TO31"},{"TABLE_NAME":"TB_CHANGE_2TO3_JS2"},{"TABLE_NAME":"TB_CHANGE_2TO3_JS21"},{"TABLE_NAME":"TB_DATACATALOG"},{"TABLE_NAME":"TB_DATARESOURCE"},{"TABLE_NAME":"TB_DATASERVER_FIELD"},{"TABLE_NAME":"TB_DATASERVER_GROUP"},{"TABLE_NAME":"TB_DATASERVER_LAYER"},{"TABLE_NAME":"TB_DATASERVER_SH"},{"TABLE_NAME":"TB_DATASOURCE"},{"TABLE_NAME":"TB_DICT_BZML"},{"TABLE_NAME":"TB_DICT_CODE"},{"TABLE_NAME":"TB_DICT_GDZZSX"},{"TABLE_NAME":"TB_DICT_KZDLX"},{"TABLE_NAME":"TB_DICT_PODU"},{"TABLE_NAME":"TB_DICT_TBXHLX"},{"TABLE_NAME":"TB_DICT_TDFL"},{"TABLE_NAME":"TB_DICT_TDFL1"},{"TABLE_NAME":"TB_DICT_TDFL_2D"},{"TABLE_NAME":"TB_DICT_TDFL_3DL"},{"TABLE_NAME":"TB_DICT_TDFX_2D"},{"TABLE_NAME":"TB_DICT_TJFXTEMPLE"},{"TABLE_NAME":"TB_DICT_XZQH"},{"TABLE_NAME":"TB_DICT_XZQHDM"},{"TABLE_NAME":"TB_LAYERSCALE"},{"TABLE_NAME":"TB_LOG"},{"TABLE_NAME":"TB_MAPXZQSCDATE"},{"TABLE_NAME":"TB_MARKMAP"},{"TABLE_NAME":"TB_NODELIST"},{"TABLE_NAME":"TB_ORGANIZATIONS"},{"TABLE_NAME":"TB_QUERY"},{"TABLE_NAME":"TB_RESOURCE"},{"TABLE_NAME":"TB_RESOURCE_PERMISSION"},{"TABLE_NAME":"TB_SERVER_MANGER"},{"TABLE_NAME":"TB_SOURCE_DATA"},{"TABLE_NAME":"TB_SOURCE_FILE"},{"TABLE_NAME":"TB_SOURCE_IMG"},{"TABLE_NAME":"TB_SOURCE_SERVER"},{"TABLE_NAME":"TB_TARGET"},{"TABLE_NAME":"TB_TITLELIST"},{"TABLE_NAME":"TB_USERCLAIMS"},{"TABLE_NAME":"TB_USERLOGINS"},{"TABLE_NAME":"TB_USERS"},{"TABLE_NAME":"TDS_LAYERASPX"},{"TABLE_NAME":"TEST"},{"TABLE_NAME":"TESTDATA"},{"TABLE_NAME":"TS_ANALYSISMANAGE"},{"TABLE_NAME":"TS_ANALYSISTYPE"},{"TABLE_NAME":"TS_BGDATAIMPORT"},{"TABLE_NAME":"TS_BGDATAIMPORTBF"},{"TABLE_NAME":"TS_CODEDIC"},{"TABLE_NAME":"TS_DATAIMPORT"},{"TABLE_NAME":"TS_DATAIMPORTBF"},{"TABLE_NAME":"TS_FIELDSDIC"},{"TABLE_NAME":"TS_FIELDSDIC_META"},{"TABLE_NAME":"TS_FILECATALOGDIC"},{"TABLE_NAME":"TS_FILEINFO"},{"TABLE_NAME":"TS_FILESERVER"},{"TABLE_NAME":"TS_FUNPRVINFO"},{"TABLE_NAME":"TS_GLTJB"},{"TABLE_NAME":"TS_GLTJBFB"},{"TABLE_NAME":"TS_HISDATAINFO"},{"TABLE_NAME":"TS_JBTJTABLEDIC"},{"TABLE_NAME":"TS_LOADLAYERMANAGE"},{"TABLE_NAME":"TS_RASETERIMPINFO"},{"TABLE_NAME":"TS_RASTERIMPBLOB"},{"TABLE_NAME":"TS_ROLEINFO"},{"TABLE_NAME":"TS_ROLEPOWER"},{"TABLE_NAME":"TS_SYMBOLDIC"},{"TABLE_NAME":"TS_SYSLOGS"},{"TABLE_NAME":"TS_USERINFO"},{"TABLE_NAME":"TS_USERPOWER"},{"TABLE_NAME":"TS_VDATADIC"},{"TABLE_NAME":"TS_VERSIONINFO"},{"TABLE_NAME":"TS_XXTJB"},{"TABLE_NAME":"TS_XXTJB_SUB"},{"TABLE_NAME":"TS_XZQHCENTERDIC"},{"TABLE_NAME":"TS_XZQHDIC"},{"TABLE_NAME":"TS_XZQZJHCENTERDIC"},{"TABLE_NAME":"T_FUWUZHUCE"},{"TABLE_NAME":"T_FUWUZHUCE1"},{"TABLE_NAME":"T_FUWUZHUCE_SUB"},{"TABLE_NAME":"T_SJZD"},{"TABLE_NAME":"T_ZDB"}]||[{"TABLENAME":"表格1"},{"TABLENAME":"表格2"},{"TABLENAME":"表格3"},{"TABLENAME":"表格4"}]'
                        var result1 = JSON.parse(result[0]),
                            result2 = JSON.parse(result[1]),
                            html1 = '',
                            html2 = '';
                        result1.map(function (item, index) {
                            html1 += '<li class="es-visible">' + item.TABLE_NAME + '</li>';
                        });
                        $('.es-list').html(html1);
                        result2.map(function (item, index) {
                            html2 += '<li class="es-visible">' + item.TABLE_NAME + '</li>';
                        });
                        searchData[dbName]['0'] = html1;
                        searchData[dbName]['1'] = html2;
                        $('.es-list').html(searchData[dbName][mySelect]);
                    } else {
                        console.log(result.exp);
                    }
                },
                error: function (err) {
                    console.log(err);
                }
            });
        }
        $('.es-list').html(searchData[dbName][mySelect]);
    }

    // 获取可查询的数据库名称列表
    $.ajax({
        type: "post",
        async: false,
        url: "/BaseAshx/SmartStatistics/ExploreData.ashx",
        data: {
            mtype: "ExploreData_GetDataBaseList"
        },
        datatype: "json",
        success: function (result) {
            result = JSON.parse(result);
            // console.log(result);
            if (result.code == 200) {
                var html = '';
                result.data.map(function (item, index) {
                    html += '<option>' + item.NAME + '</option>';
                    searchData[item.NAME] = {
                        '0': '', // 数据库表
                        '1': '' // 历史数据关联表
                    };
                });
                $('#dbName').html(html);
                dbName = $('#dbName').val();
                initData();
                // console.log(searchData)
            } else {
                console.log(result.exp);
            }
        },
        error: function (err) {
            console.log(err);
        }
    });

    $('.nav-tabs li').on('click', function () {
        setTimeout(function () {
            $('#tab-span .fa').click();
        }, 200);
    });
});